<?php

namespace App\Http\ApiControllers;

use App\Facades\Api;
use App\Http\Resources\GiftResource;
use App\Models\Gift;
use App\Models\Order;
use App\Models\Player;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class GiftController extends ApiController
{
    public function index(Player $player): JsonResponse
    {
        $list = $player->season->gifts()->ofEnabled()->ordered()->get();

        return $this->success(GiftResource::collection($list));
    }

    /**
     * 创建赞助订单
     *
     * @param  Request  $request
     * @param  Player  $player
     * @param  Gift  $gift
     * @return JsonResponse
     */
    public function store(Request $request, Player $player, Gift $gift): JsonResponse
    {
        $qty = $request->qty;

        $order = Order::create([
            'application_id' => 1,
            'season_id' => $player->season_id,
            'chapter_id' => $player->chapter_id,
            'player_id' => $player->id,
            'user_id' => Api::id(),
            'wechat_id' => $player->getWechatId(),
            'payment_id' => $player->getPaymentId(),
            'gift_id' => $gift->id,
            'qty' => $qty,
            'amount' => $gift->price * $qty,
        ]);

        return $this->orderResponse($order, $player);
    }
}
